package org.example.springmybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.example.springmybatis.modle.UserInfo;

import java.util.List;
@Mapper   //交给Spring管理了
public interface UserInfoMapper {
    @Select("select * from student")
    List<UserInfo> selectAll();   //通过这个方法来访问数据库

    @Select("select * from student where age = #{AGE}")  //这里是进行参数绑定的
    UserInfo selectAllById(@Param("AGE") Integer age);  //@Param 对参数重命名

    //数据库新增

    @Options(useGeneratedKeys = true, keyProperty = "id")   //这个注解是为了设置主键 Id 的 获取自增Id
    @Insert("insert into student (age , name) VALUES(#{age}, #{name})")
    Integer insertUser(UserInfo userInfo);  // 直接传对象，用对象接收

    //数据库删除
    @Delete("delete from student where age = #{age}")
    Integer delectUserById(Integer age);

    //更新数据库
    @Update("update student set age = #{age} where name = #{name} ")
    Integer uddateuserinfo(UserInfo userInfo);

    //动态SQL
    @Insert("<script>" +
            " insert into student"+
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"\n" +
            "(\"  suffix=\")\">"+
            "<if test=\"age != null\">age</if>"+
            "<if test=\"name != null\"> name</if>"+
            "</trim>  VALUES\"" +
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">"+
            " <if test=\"age != null\">#{age}</if>"+
            "<if test=\"name != null\">#{name}</if>"+
            " </trim>"+
            "</script>")
    Integer InsereUser3(UserInfo userInfo);
}
